NetBeans "no main classes found"

来源:百度知道 编辑:UC知道 时间:2024/09/18 06:19:55
用的是NetBeans6.7.1
照书上的例子新建了Project然后写了Example里的内容。
写完按F6 output之后,弹出对话框说,"No main classes found",可是写了Elevator的class啊?
请问我有哪里没做对?
加上软件里本来给出的Template,右边屏幕显示如下:

} * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package elevatortest1;

/**
*
* @author Yang Xiaoyu
*/
public class Elevator {
public boolean doorOpen=false;//Doors are closed by default
public int currentFloor=1;//All elevators start on first floor
public final int TOP_FLOOR=10;
public final int MIN_FLOOR=1;

/**
* @param args the command line arguments
*/
public void openDoor() {
System.out.println("Opening door.");
doorOpen=true;
System.out.println("Door is open.");
}
public void closeDoor(){
System.out.println("Closing dor

运行java项目的时候必须有一个main方法才能运行。
main方法的签名为public static void main(String[] args);其中最关键的是static,以为该方法或字段不必需要对象的创建才能创建,而是只要一个类就行了。我们都知道当一个项目刚运行的时候是没有任何对象生成的,那这是项目是怎样初始化的呢?这就是static的作用了。所以你需要在类中加一个public static void main(String[] args)主方法或者写一个static函数初始化所有对象都可以解决这个问题。
表达能力实在不怎么样……

类Elevator中没有main方法,有main方法的类才是main class